﻿// Copyright (c) Microsoft Corporation
//
// All rights reserved. 
//
// Licensed under the Apache License, Version 2.0 (the ""License""); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING 
// WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY 
// OR NON-INFRINGEMENT. 
//
// See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.

using DPE.TED.OCAF.Win8SampleClient.Common;
using System;
using System.Diagnostics;
using Windows.Devices.Input;
using Windows.UI.Input;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Navigation;
using DPE.TED.OCAF.Win8SampleClient.ViewModels;
using GalaSoft.MvvmLight.Messaging;
using DPE.TED.OCAF.Win8SampleClient.UI_Helpers;
using System.Threading.Tasks;

namespace DPE.TED.OCAF.Win8SampleClient
{
    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    public sealed partial class RenderPage : Page
    {

        private NavigationHelper navigationHelper;
        private ObservableDictionary defaultViewModel = new ObservableDictionary();

        /// <summary>
        /// This can be changed to a strongly typed view model.
        /// </summary>
        public ObservableDictionary DefaultViewModel
        {
            get { return this.defaultViewModel; }


        }

        /// <summary>
        /// NavigationHelper is used on each page to aid in navigation and 
        /// process lifetime management
        /// </summary>
        public NavigationHelper NavigationHelper
        {
            get { return this.navigationHelper; }
        }

        /// <summary>
        ///  Initialize page and populate listview controls
        /// </summary>
        public RenderPage()
        {
            this.InitializeComponent();
            this.navigationHelper = new NavigationHelper(this);

            Messenger.Default.Register<DialogMessage>
                (
                     this,
                     ( action ) => ReceiveMessage( action )
                );
        }

        private async Task<object> ReceiveMessage( DialogMessage action )
        {
            // Display Modal Dialog
            var msg = new Windows.UI.Popups.MessageDialog( action.Text );
            await msg.ShowAsync();
            return null;
        }

        #region NavigationHelper registration

        /// The methods provided in this section are simply used to allow
        /// NavigationHelper to respond to the page's navigation methods.
        /// 
        /// Page specific logic should be placed in event handlers for the  
        /// <see cref="GridCS.Common.NavigationHelper.LoadState"/>
        /// and <see cref="GridCS.Common.NavigationHelper.SaveState"/>.
        /// The navigation parameter is available in the LoadState method 
        /// in addition to page state preserved during an earlier session.

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            navigationHelper.OnNavigatedTo(e);

            ((RenderPageViewModel)this.DataContext).Initialize((PdfViewModel)e.Parameter);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            navigationHelper.OnNavigatedFrom(e);
        }

        #endregion

        private void image_RightTapped(object sender, RightTappedRoutedEventArgs e)
        {
            // Right Tapped is triggered during a press and hold or a right click
            Debug.WriteLine("RightTapped: " + e.PointerDeviceType);

            int page = ((PDFPageViewModel)((Image)sender).DataContext).Page;
            if (e.PointerDeviceType == PointerDeviceType.Mouse)
            {
                ((RenderPageViewModel)this.DataContext).AddComment("", e.GetPosition(this).X, e.GetPosition(this).Y, page, ((Image)sender).ActualWidth, ((Image)sender).ActualHeight);
               
            }
        }

        private void image_Holding(object sender, HoldingRoutedEventArgs e)
        {
            Debug.WriteLine("Holding: " + e.HoldingState);

            if (e.HoldingState == HoldingState.Started)
            {

                int page = ((PDFPageViewModel)((Image)sender).DataContext).Page;
                ((RenderPageViewModel)this.DataContext).AddComment("", e.GetPosition(this).X, e.GetPosition(this).Y, page, ((Image)sender).ActualWidth, ((Image)sender).ActualHeight);
            }
        }

        private void pageRoot_Unloaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            ((RenderPageViewModel)this.DataContext).CancelAllRendering();
        }
    }
}
